<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/22
 * Time: 18:03
 */

namespace app\api\controller;


class Version
{
    /**
     * http://127.0.0.1:9998/api/version/soft?curr_ver=1&curr_os=1
     * @return \think\response\Json
     */
    public function soft()
    {
        $curr_ver = trim(input("request.curr_ver"));//当前版本号
        $curr_os = trim(input("request.curr_os"));//1:ios,2:android
        $ver = trim(input("request.ver"));//班级id
        $imei = trim(input("request.imei"));//班级id
        $os = trim(input("request.os"));//班级id
        $language = trim(input("request.language"));//language=2表示英文 其他表示中文

        $where = "";
        if ($curr_os == "1") {
            $where .= " plat=1 ";
        } else if ($curr_os == "2") {
            $where .= " plat=2 ";
        } else {
            $return['err'] = 1;
            $return['msg'] = '参数错误';
            return json($return);
        }

        $where .= " and version>" . $curr_ver;
        if($language==2){
            $where .= " and lang=1 ";
        }else
        {
            $where .= " and lang=0 ";
        }
        $db = \think\Db::connect("DB_SLEEP_VERSION");
        $sql = "select * from sleep_version.version where " . $where . " order by version desc";
        $list =  $db->query($sql);
        $rdata = array();
        foreach ($list as $key => $val) {
            if ($val['version'] > $curr_ver) {
                if (empty($rdata)) {
                    $rdata = $list[$key];
                    break;
                } else {
                    if ($val['vtype'] == 0) {
                        $rdata = $list[$key];
                        break;
                    }
                }
            }
        }
        if (empty($rdata)) {
            $rdata['has_package'] = 0;
        } else {
            $rdata['has_package'] = 1;
        }
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $rdata;
        return json($return);
    }

    public function getSoftUrl()
    {
        $language = trim(input("request.language"));//language=2表示英文 其他表示中文
        if($language==2){
            $db = \think\Db::connect("DB_SLEEP_VERSION");
            $sql="select * from sleep_version.version where plat=1 and lang=1 order by version desc";
            $ios=$db->query($sql);
            $sql="select * from sleep_version.version where plat=2 and lang=1 order by version desc";
            $android=$db->query($sql);
        }else
        {
            $db = \think\Db::connect("DB_SLEEP_VERSION");
            $sql="select * from sleep_version.version where plat=1 and lang=0 order by version desc";
            $ios=$db->query($sql);
            $sql="select * from sleep_version.version where plat=2 and lang=0 order by version desc";
            $android=$db->query($sql);
        }


        $rdata['ios']=$ios[0]['link'];
        $rdata['android']=$android[0]['link'];
        $return['err']=0;
        $return['msg']='获取成功';
        $return['data']=$rdata;
        return json($return);
    }
}